package czh.cocoa.ui.impl;

import czh.cocoa.foundation.NSString;
import czh.cocoa.graphics.CGRect;
import czh.cocoa.graphics.CGSize;
import czh.cocoa.ui.UIImage;
import czh.cocoa.ui.UIView;

public class UIProgressView extends UIView {

	private float mProgress;
	
	private UIImageView mProgressView;
	
	private UIImageView mTrackView;
	
	public UIProgressView() {
		
	}
	
	public UIProgressView(CGRect frame) {
		setFrame(frame);
	}
	
	protected void init() {
		super.init();
		
		UIImage track_img = UIImage.imageNamed(NSTR("cocoa/progress_track")); 
		UIImageView track_view = new UIImageView(track_img);
		track_view.setAutoresizingMask(UIViewAutoresizingFlexibleSize);
		track_view.setContentStretch(CGRectMake(0.4f, 0, 0.1f, 1));
		addSubview(track_view);
		
		UIImage progress_img = UIImage.imageNamed(NSTR("cocoa/progress")); 
		UIImageView progress_view = new UIImageView(progress_img);
		progress_view.setAutoresizingMask(UIViewAutoresizingFlexibleHeight);
		progress_view.setContentStretch(CGRectMake(0.4f, 0, 0.1f, 1));
		addSubview(progress_view);
		
		CGRect frame = progress_view.frame();
		frame.size.width = 0;
		progress_view.setFrame(frame);
		
		mTrackView = track_view;
		mProgressView = progress_view;
	}
	
	public void setProgress(float progress) {
		if (progress > 1) {
			progress = 1;
		} else if (progress < 0) {
			progress = 0;
		}
		if (mProgress != progress) {
			mProgress = progress;
			CGRect frame = mProgressView.frame();
			float min_width = mProgressView.image().size().width;
			frame.size.width = Math.max(bounds().size.width * progress, min_width);
			mProgressView.setFrame(frame);
		}
	}
	
	public void setProgressImage(UIImage image) {
		mProgressView.setImage(image);
	}
	
	public UIImage progressImage() {
		return mProgressView.image();
	}
	
	public void setTrackImage(UIImage image) {
		mTrackView.setImage(image);
	}
	
	public UIImage trackImage() {
		return mTrackView.image();
	}
	
	public void setContentStretch(CGRect stretch) {
		super.setContentStretch(stretch);
		mProgressView.setContentStretch(stretch);
		mTrackView.setContentStretch(stretch);
	}
	
}
